package bookManageSystem.view;

import bookManageSystem.Mat.StaticMapExample;
import bookManageSystem.tools.ComponentTools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainFrame extends JFrame implements ActionListener {
    private ComponentTools componentTools = new ComponentTools();
    private UserDetailsPanel userDetailsPanel = new UserDetailsPanel();
    private OrderView ordersView =  new OrderView();
    private EmployeeView employmentView =   new EmployeeView();
    private DepartmentSelectPanel departmentsSelectPanel =  new DepartmentSelectPanel();
    private JMenuBar menuBar;
    private JMenu userMenu, medicalInfoMenu, adminMenu;
    private JMenuItem loginMenuItem, departmentSelectMenuItem,
            viewOrdersMenuItem, viewUsersMenuItem;

    MainFrame(String title) {
        this.setTitle("医疗信息管理系统");
        this.setBounds(400, 400, 800, 700);

        // 为主界面添加菜单条
        this.setJMenuBar(this.createMenuBar());
        // 设置默认面板（例如登录面板）
        this.setContentPane(userDetailsPanel);
        userDetailsPanel.setUserDetails(); // 设置示例用户详情
        // 假设你有一个 LoginPanel 类
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true); // 初始设置为可见

        // 为菜单条中的菜单项注册事件
        loginMenuItem.addActionListener(this);
        departmentSelectMenuItem.addActionListener(this);
        //点击医疗管理信息
        medicalInfoMenu.addActionListener(this);
        viewOrdersMenuItem.addActionListener(this);
        viewUsersMenuItem.addActionListener(this);
    }

    /**
     * 为主界面创建一个菜单条
     *
     * @return 返回菜单条
     */
    private JMenuBar createMenuBar() {
        menuBar = new JMenuBar();


        String userType = StaticMapExample.get("userType");
        if (!userType.equals("管理员")){
            // 【用户】菜单
            userMenu = new JMenu("用户信息");
            loginMenuItem = new JMenuItem("用户详情");
            userMenu.add(loginMenuItem);
            menuBar.add(userMenu);

            // 【医疗信息管理】菜单
            medicalInfoMenu = new JMenu("医疗信息管理");
            departmentSelectMenuItem = new JMenuItem("选择科室");
            medicalInfoMenu.add(departmentSelectMenuItem);
            menuBar.add(medicalInfoMenu);

        }else {

            // 【管理员】菜单
            adminMenu = new JMenu("管理员");
            viewOrdersMenuItem = new JMenuItem("查询所有预约订单");
            viewUsersMenuItem = new JMenuItem("查询所有用户信息");
            adminMenu.add(viewOrdersMenuItem);
            adminMenu.add(viewUsersMenuItem);
            menuBar.add(adminMenu);


            // 【用户】菜单
            userMenu = new JMenu("用户信息");
            loginMenuItem = new JMenuItem("用户详情");
            userMenu.add(loginMenuItem);
            menuBar.add(userMenu);
            userMenu.setVisible(false);

            // 【医疗信息管理】菜单
            medicalInfoMenu = new JMenu("医疗信息管理");
            departmentSelectMenuItem = new JMenuItem("选择科室");
            medicalInfoMenu.add(departmentSelectMenuItem);
            menuBar.add(medicalInfoMenu);
            medicalInfoMenu.setVisible(false);
        }




        return menuBar;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == loginMenuItem) {
            System.out.println("用户详情");
            // 将主界面的内容面板替换成图书类别添加面板
            this.setContentPane(new UserDetailsPanel());
            // 并设置该面板显示
            userDetailsPanel.setUserDetails();
            this.setVisible(true);
        }
        if (e.getSource() == departmentSelectMenuItem) {
            System.out.println("点击医疗管理信息");
            // 将主界面的内容面板替换成图书类别添加面板
            this.setContentPane(new DepartmentSelectPanel());
            // 并设置该面板显示
            this.setVisible(true);

        }

        if (e.getSource() == viewOrdersMenuItem) {

            // 将主界面的内容面板替换成图书类别添加面板
            this.setContentPane(new OrderView());
            // 并设置该面板显示
            ordersView.getOrderView();
            this.setVisible(true);

        }
        if (e.getSource() == viewUsersMenuItem) {
            // 将主界面的内容面板替换成
            this.setContentPane(new EmployeeView());
            // 并设置该面板显示
            employmentView.getOrderView();
            this.setVisible(true);
        }
    }
}